<template>
  <div class="dict-page page-main">
    <search-form :model="searchParams" @search="search">
      <a-form-item label="字典名称" name="name">
        <a-input v-model:value="searchParams.name" placeholder="请输入" />
      </a-form-item>
    </search-form>
    <div class="table-toolbar">
      <add-button @click="add()" />
    </div>
    <g-table
      ref="tableRef"
      sort=""
      :columns="columns"
      :api="getDicts"
      :params="searchParams"
    />

    <DictDetail v-model:visible="visible" :data="activeRow" @change="search(false)" />
  </div>
</template>

<script setup lang="tsx">
import { deleteDictById, getDicts } from './api';
import type { IDict } from './entity';
import DictDetail from './detail.vue';

const searchParams = reactive({
  name: ''
})

let visible = $ref(false)

const { tableRef, columns, search } = useTable<IDict>([
  { title: '字典名称', dataIndex: 'name' },
  { title: '字典类型', dataIndex: 'type' },
  { title: '字典码', dataIndex: 'code' },
  { title: '字典值', dataIndex: 'value' },
  { title: '排序', dataIndex: 'orderNum' },
  { title: '备注', dataIndex: 'remark' },
  {
    title: '操作',
    dataIndex: 'op',
    customRender: ({ record }) => (
      <>
        <table-button onClick={() => add(record)}>编辑</table-button>
        <confirm-button danger onClick={() => remove(record)}>删除</confirm-button>
      </>
    )
  }
])

let activeRow = $ref<Partial<IDict>>({})
function add(row: Partial<IDict> = {}) {
  activeRow = {...row}
  visible = true
}

function remove({ id }: IDict) {
  deleteDictById(id as number).then(() => {
    AntMessage.success('已删除')
    search(false)
  })
}
</script>

<style lang="less"></style>